1783C - Yet Another Tournament - CodeForces Solution


binary search greedy sortings

Please click on ads to support us..

Python Code:


def ii(num=False):
    i = input().split()
    if num:
        return int(i[0])
    try:
        return list(map(int, i))
    except Exception:
        return i


def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)

for _ in range(ii(1)):
    n, m = ii()
    a = ii()
    b = list(a)
    b.sort()
    ans = 0
    for i in b:
        if i <= m:
            ans+=1
            m-=i
        else:
            break
    
    if ans != n and ans != 0 and b[ans-1] + m >= a[ans]:
        ans+=1
    print(n + 1 - ans)

C++ Code:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    int t;
    cin >> t;
    while(t--) {
        int n, m;
        cin >> n >> m;
        vector<pair<int, int>> A(n);
        for(int i=0; i<n; i++) {
            cin >> A[i].first;
            A[i].second = i;
        }
        sort(A.begin(), A.end());
        int sum = 0, cnt = 0;
        for(int i=0; i<n; i++) {
            if(sum + A[i].first > m)
                break;
            sum += A[i].first;
            cnt++;
        }
        int idx = -1;
        for(int i=0; i<n; i++) {
            if(A[i].second == cnt) {
                idx = i;
                break;
            }
        }
        if(cnt == 0) {
            cout << n+1 << '\n';
        } else if(cnt == n) {
            cout << 1 << '\n';
        } else {
            if(sum - A[cnt - 1].first + A[idx].first > m) 
                cout << n - cnt + 1 << '\n';
            else 
                cout << n - cnt << '\n';
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess